package com.aramis.mydytt.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import com.aramis.mydytt.R

/**
 * Description: BottomDeleteView
 * @author lizhidan
 * Created: 2021/6/7
 */
class BottomDeleteView : RelativeLayout {
    constructor(context: Context?) : super(context) {
        init(null, 0)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs, 0)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(attrs, defStyleAttr)
    }

    private lateinit var selectImageView: AppCompatImageView
    private lateinit var selectAllLayout: RelativeLayout
    private lateinit var deleteView: TextView
    var allSelect = false
        private set
    private var selectColor: Int = 0

    private fun init(attrs: AttributeSet?, defStyleAttr: Int) {
        inflate(context, R.layout.view_bottom_delete, this)
        setBackgroundColor(Color.WHITE)
        selectAllLayout = findViewById(R.id.layout_bottom_delete_select_all)
        selectImageView = findViewById(R.id.image_bottom_delete_select_all)
        deleteView = findViewById(R.id.text_bottom_delete)
        selectColor = ContextCompat.getColor(context, R.color.color_select)
    }

    fun setAllSelectClickListener(listener: ((Boolean) -> Unit)? = null) {
        selectAllLayout.setOnClickListener {
            allSelect = !allSelect
            selectImageView.setImageResource(if (allSelect) R.drawable.ic_select_tap else R.drawable.ic_select_nor)
            selectImageView.setColorFilter(if (allSelect) selectColor else Color.BLACK)
            listener?.invoke(allSelect)
        }
    }

    fun setOnDeleteClickListener(listener: View.OnClickListener) {
        deleteView.setOnClickListener(listener)
    }
}